<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="计划名称" >
        <el-input v-model="form.planName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检对象id" >
        <el-input v-model="form.objId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检对象" >
        <el-input v-model="form.objName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检模板id" >
        <el-input v-model="form.templateId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检模板名称" >
        <el-input v-model="form.templateName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检计划说明" >
        <el-input v-model="form.planDesc" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检周期id" >
        <el-input v-model="form.periodId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检周期名称" >
        <el-input v-model="form.periodName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检人id" >
        <el-input v-model="form.inspectorId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检人名称" >
        <el-input v-model="form.inspectorName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检车辆类型id" >
        <el-input v-model="form.vehTypeId" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检车辆类型m名称" >
        <el-input v-model="form.vehTypeName" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="开始时间" >
        <el-input v-model="form.startTime" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="结束时间" >
        <el-input v-model="form.endTime" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检时间" >
        <el-input v-model="form.operateTime" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="任务状态" >
        <el-input v-model="form.state" style="width: 370px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/tInspectionTask'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehTypeId: '',
        vehTypeName: '',
        startTime: '',
        endTime: '',
        operateTime: '',
        state: ''
      },
      rules: {
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        planName: '',
        objId: '',
        objName: '',
        templateId: '',
        templateName: '',
        planDesc: '',
        periodId: '',
        periodName: '',
        inspectorId: '',
        inspectorName: '',
        vehTypeId: '',
        vehTypeName: '',
        startTime: '',
        endTime: '',
        operateTime: '',
        state: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
